<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;
class HUserController extends HomebaseController {
	function index(){
		$this->ajaxReturn(sp_ajax_return('siccess','success',1));
	}
	
	function login(){
		$this->display();
	}
	
	function test(){
		var_dump(sp_get_current_user());
	}
	function protocol(){
	  $this->version = $this->js_version();
		$this->display();
	}
	
	function user(){
		$user=sp_get_current_user();
//		if($user==false){
//			$this->login = null;
//		}else{
//			$this->login = 1;
//		}
		$this->user = $user;
    $this->version = $this->js_version();
    $this->hidead = $this->hide_ad();
		$this->display();
	}
	
	function exchange(){
		$user=sp_get_current_user();
//		if($user==false){
//			redirect("/HUser/login");
//		}
		$Const = M("Constant");
		$this->user = $user;
		$lo =$Const->where("pname='".$user['grade']."'")->select();
		$this->lo = $lo[0]['listorder'];
		$this->grade = $Const->where("parentid =1")->order("listorder")->select();
    $this->version = $this->js_version();
		$this->display();
	}
	function option(){
		$user=sp_get_current_user();
		$this->visible = $user['is_visible'];
    $this->version = $this->js_version();
		$this->display();
	}
	function aboutus(){
	  $this->version = $this->js_version();
		$this->display();
	}
	function recharge(){
	  $this->version = $this->js_version();
		session("needpay",1);
		$this->display();
	}
	// 我的广告
	function subscribe(){
		$user=sp_get_current_user();
//		if($user==false){
//			redirect("/HUser/login");
//		}
		$sql = "select a.*,c.icon,b.id as st_id from m_ad as a join m_stand as b join m_img_lib as c ".
		"on a.stand_id = b.id and a.id = c.ad_id where a.user_uniq_id = '".$user['user_uniq_id'].
		"' GROUP BY st_id order BY listorder asc";
    $publishres= M("Publish")->field("*,'1' as type")->where("user_uniq_id ='".$user['user_uniq_id']."'")->order("create_time desc")->select();
		$res = M()->query($sql);
    foreach($publishres as $p)array_push($res,$p);
		$this->ad = $res;
    $this->version = $this->js_version();
		if(empty($res)){
			$this->redirect("Portal/HUser/null_ad");
			die;
		}
		$this->display();
	}
	
	function null_ad(){
	  $this->version = $this->js_version();
		$this->display();
	}
	function userinfo(){
		$user=sp_get_current_user();
//		if($user==false){
//			redirect("/HUser/login");
//		}
		if(empty($user['grade'])||strlen($user['grade'])==0)$this->grade = "去兑换";
		else $this->grade = $user['grade'];
		$this->userpost = session("userpost");
		$this->user = $user;
    $this->version = $this->js_version();
    $this->hidead = $this->hide_ad();
		$this->display();
	}
	
	function userinfo_edit_post(){
		session("userpost",null);
		$icon = $_POST['icon'];
		try{
			$user=sp_get_current_user();
			$Appu = M("AppUser");
			$data=$Appu->create();
			$nick = $Appu->where("nick = '".$data['nick']."'")->count(1);
			if($nick>0&&$data['nick']!=$user['nick'])$this->error("昵称已存在");
			if(!$data){
				session("userpost",1);
				redirect("userinfo");
				return;
			}
			$data['icon'] = $icon;
			$res = $Appu->where("user_uniq_id =".$data['user_uniq_id'])->data($data)->save();
			if($res===false){
				$this->error("修改失败");
			}
			else{
				session("user",$Appu->where("user_uniq_id =".$data['user_uniq_id'])->find());
				redirect("user");
			}
		}catch(Exception $e){
			session("userpost",1);
			redirect("userinfo");
		}
	}
	
	
	function register(){
	  $this->version = $this->js_version();
		$this->display();
	}
	
	function resetpwd(){
	  $this->version = $this->js_version();
		$this->display();
	}
}